package flc.ast.dialog;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.AddBackgroundActivity;
import flc.ast.activity.AddFilterActivity;
import flc.ast.activity.AddStickerActivity;
import flc.ast.activity.AddTextActivity;
import flc.ast.activity.AlbumActivity;
import flc.ast.activity.PicCutActivity;
import flc.ast.activity.PicPuzzleActivity;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.DialogAddPicBinding;
import flc.ast.databinding.DialogSelPicBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicDialog extends BaseSmartDialog<DialogSelPicBinding> implements View.OnClickListener {

    public int type;
    private SelPictureAdapter pictureAdapter;
    private List<String> listSel = new ArrayList<>();
    private String selPath = "";
    private int oldPosition;

    public SelPicDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sel_pic;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvDialogSelPicImport.setOnClickListener(this);
        mDataBinding.ivDialogSelPicCancel.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        pictureAdapter = new SelPictureAdapter();
        pictureAdapter.setManage(true);
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (type == 0) {
                    if (pictureAdapter.getItem(position).isChecked()) {
                        pictureAdapter.getItem(position).setChecked(false);
                        listSel.remove(pictureAdapter.getItem(position).getPath());
                    } else {
                        pictureAdapter.getItem(position).setChecked(true);
                        listSel.add(pictureAdapter.getItem(position).getPath());
                    }
                    pictureAdapter.notifyItemChanged(position);
                } else {
                    pictureAdapter.getItem(oldPosition).setChecked(false);
                    pictureAdapter.notifyItemChanged(oldPosition);
                    oldPosition = position;
                    pictureAdapter.getItem(position).setChecked(true);
                    pictureAdapter.notifyItemChanged(position);
                    selPath = pictureAdapter.getItem(position).getPath();
                }
            }
        });


        getPermission();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvDialogSelPicImport:
                if (listSel.size() == 0 && selPath.isEmpty()) {
                    ToastUtils.showShort(getContext().getString(R.string.please_sle_pic));
                    return;
                }
                if (type == 0) {
                    PicPuzzleActivity.listPath = listSel;
                    getContext().startActivity(new Intent(getContext(), PicPuzzleActivity.class));
                } else if (type == 1) {
                    PicCutActivity.imgPath = selPath;
                    getContext().startActivity(new Intent(getContext(), PicCutActivity.class));
                } else if (type == 2) {
                    AddFilterActivity.imgPath = selPath;
                    getContext().startActivity(new Intent(getContext(), AddFilterActivity.class));
                } else if (type == 3) {
                    AddStickerActivity.imgPath = selPath;
                    getContext().startActivity(new Intent(getContext(), AddStickerActivity.class));
                } else if (type == 4) {
                    AddTextActivity.imgPath = selPath;
                    getContext().startActivity(new Intent(getContext(), AddTextActivity.class));
                } else if (type == 5) {
                    AddBackgroundActivity.imgPath = selPath;
                    getContext().startActivity(new Intent(getContext(), AddBackgroundActivity.class));
                }
                dismiss();
                break;
            case R.id.ivDialogSelPicCancel:
                dismiss();
                break;
        }
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getContext().getString(R.string.get_picture_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvPictureList.setVisibility(View.GONE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getContext(), PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void clickMultiple();

        void clickSingle();
    }
}
